package com.east.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.ContentType;
import com.alibaba.excel.EasyExcel;
import com.east.dto.ClueDTO;
import com.east.dto.CustomerDTO;
import com.east.entity.Clue;
import com.east.entity.Customer;
import com.east.entity.Product;
import com.east.entity.User;
import com.east.mapper.CustomerMapper;
import com.east.mapper.ProductMapper;
import com.east.query.CustomerQuery;
import com.east.result.Result;
import com.east.service.ClueService;
import com.east.service.CustomerService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Component
public class CustomerManager {
    @Resource
    private CustomerService customerService;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private ClueService clueService;
    @Resource
    private ProductMapper productMapper;

    @Transactional
    public Result clue2customer(CustomerDTO customerDTO) {
        // 1. 先改变线索状态
        clueService.updateState(customerDTO.getClueId(), -1);
        // 2. 保存客户信息
        customerService.save(customerDTO);
        return Result.OK();
    }

    public void all2excel(HttpServletResponse response, String ids) throws IOException {
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(ids);

        CustomerQuery customerQuery = new CustomerQuery();
        if (ids != null && ids != "") {
            String[] idArr = ids.split(",");
            customerQuery.setIds(idArr);
        }
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType(ContentType.OCTET_STREAM.toString());
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(loginUser.getName() + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(
                response.getOutputStream(),
                CustomerDTO.class).sheet("模板").doWrite(customerMapper.list(customerQuery));
    }

    public void selected2excel(HttpServletResponse response, String ids) {
        System.out.println(ids);
    }

    public Result info(Integer id) {
        CustomerDTO customerDTO = customerMapper.info(id);
        ClueDTO customerClue = (ClueDTO) clueService.clueCascadeInfo(customerDTO.getClueId()).getData();
        BeanUtil.copyProperties(customerDTO,customerClue);
        if (customerDTO.getProduct() == null) {
            customerClue.setIntentionProduct(null);
            customerClue.setIntentionProductValue("");
        }else {
            Product product = productMapper.selectByPrimaryKey(customerDTO.getProduct().longValue());
            customerClue.setIntentionProduct(product.getId());
            customerClue.setIntentionProductValue(product.getName());
        }

        System.out.println(customerClue);
        return Result.OK(customerClue);
    }
}
